<?php
// 引入公共文件
include_once('./config/config.php');
// 引入数据库配置文件
include_once('./config/check.php');

$depsql = "select * from per_department";
$depres = all($depsql);

$jobsql = "select * from per_job";
$jobres = all($jobsql);

if ($_POST)
{
    $data = $_POST;

    $result = add('per_person', $data);

    if (!$result)
    {
      ShowMsg('添加失败');
      exit;
    }
  
    ShowMsg('添加成功', 'PersonList.php');
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
  <head>
  <?php include_once('common/meta.php');?>
  </head>
  <body> 
    
    <?php include_once('common/header.php'); ?>

    <?php include_once('common/menu.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">添加管理员</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.html">Home</a> <span class="divider">/</span></li>
            <li class="active">Index</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                    
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='adminList.php'"><i class="icon-list"></i> 管理员列表</button>
                  <div class="btn-group">
                  </div>
                </div>

                <div class="well">
                    <div id="myTabContent" class="tab-content">
                      <div class="tab-pane active in" id="home">
                        <form method="post" enctype="multipart/form-data">
                            <label>用户名</label>
                            <input type="text" class="input-xlarge" name="name">
                            <span id="tips" style="display: none;"></span>
                            <label>头像</label>
                            <input type="file"  name="avatar">
                            <label></label>
                            <label for="">手机号</label>
                            <input type="text" name="mobile">
                            <label for="">性别</label>
                            <select name="sex" id="">
                                <option value="0">未知</option>
                                <option value="1">男</option>
                                <option value="2">女</option>
                            </select>
                            <label for="">邮箱</label>
                            <input type="text" name="email"> 
                            <label for="">部门</label>
                            <select name="depid" id="depid">
                                <?php foreach($depres as $item) { ?>
                                <option value="<?php echo $item['id'] ?>"><?php echo $item['name'] ?></option>
                                <?php } ?>
                            </select>
                            <label for="">职位</label>
                            <select name="jobid" id="jobid">

                                
                            </select>
                            <label for="">地址</label>
                            <input type="text" name="address">
                            <img src="" alt="" id="preview" style="width: 100px;"> 
                            <label></label>
                            <input class="btn btn-primary" type="submit" value="提交" />
                        </form>
                      </div>
                  </div>
                </div>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h3 id="myModalLabel">Delete Confirmation</h3>
                  </div>
                  <div class="modal-body">
                    
                    <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?</p>
                  </div>
                  <div class="modal-footer">
                    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                    <button class="btn btn-danger" data-dismiss="modal">Delete</button>
                  </div>
                </div>
                
                <?php include_once('common/footer.php'); ?>
                    
            </div>
        </div>
    </div>
  </body>
</html>
<?php include_once('common/script.php');?>

<script>
  $(document).ready(function() {
    //监听部门选择框的变化事件
    $('#depid').change(function(){
      var depid = $(this).val();
      //发送Ajax请求获取对应部门的职位信息
      $.ajax({
        type: 'get',
        url: 'getJobsByDep.php',
        data: {
          depid: depid
        },
        dataType: 'json',
        success: function(jobs) {
          //清空职位选择栏
          $('#jobid').empty();
          //动态添加选项
          $('#jobid').append('<option value="">请选择</option>')
          $.each(jobs, function(index, job) {
            $('#jobid').append('<option value="' + job.id + '">' + job.name + '</option>');
          });
        },
        error: function() {
          alert('获取职位信息失败');
        }
      })
    })
  })
</script>

